﻿/*
 * 编写人：孔辉
 * 时间：2017/5/12
 */
using FPC.Offer.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace FPC.Offer.UI.GeneralProcessor.Message
{
    /// <summary>
    /// Details 的摘要说明
    /// </summary>
    public class Details : IHttpHandler, IReadOnlySessionState
    {
        FPC.Offer.Service.Message_AddMessageService SendMessage = new Service.Message_AddMessageService();
        FPC.Offer.Service.Message_ReceiveMessageService PoliceMessage = new Service.Message_ReceiveMessageService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var FromUserName = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER].ToString();
            
            
            var Actioon = context.Request["Actioon"].ToString();//枚举判断进入哪个方法
            //显示详情
            if (Actioon == EnumAction.Select.ToString())
            {
                Guid ReceiveId = Guid.Parse(context.Request["ReceiveId"]);
                var liss = PoliceMessage.Select(p => p.ReceiveID == ReceiveId);
                var list = (
                     from n in liss
                     join w in SendMessage.Select()
                     on n.MessageID equals w.MessageId
                     select new
                     {
                         ReceiveID = n.ReceiveID,
                         MessageState = n.MessageState,
                         FromUser = w.FromUser,
                         MessageTitle = w.MessageTitle,
                         MessageContent = w.MessageContent,
                         MessageDate = w.MessageDate,
                         MessageID=w.MessageId,
                         ToUser = w.ToUser,                
                     }).ToList();
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            if (Actioon == EnumAction.Update.ToString())
            {
                ////标位已读
                Guid ReceiveId = Guid.Parse(context.Request["ReceiveId"]);
                Model.Message_ReceiveMessage Model=PoliceMessage.Find(p=>p.ReceiveID==ReceiveId).FirstOrDefault();
                Model.ReceiveID=ReceiveId;
                Model.MessageID=Model.MessageID;
                Model.MessageState="已读";
                int i= PoliceMessage.Update(Model);
                context.Response.Write(i);           
            }
            //垃圾箱
            if(Actioon == "reshow")
            {
                var pp = SendMessage.Select();
                var n = pp.Where(p => p.FromUser.Replace(" ", "") == FromUserName && p.SendState.Replace(" ", "")=="已删除").ToList();
                
                var m= PoliceMessage.Select().ToList();
                var list = (
                     from a in n
                     join w in m
                     on a.MessageId equals w.MessageID
                     select new
                     {
                          zid = a.MessageId,
                          MessageDate=a.MessageDate,//日期
                          MessageContent= a.MessageContent,//内容
                          MessageTitle= a.MessageTitle,//标题
                          SendState="发信件状态："+a.SendState,//发信息状态
                          ToUser= a.ToUser,//收件人
                          FromUser=a.FromUser,//发件人
                          ReceiveID=w.ReceiveID,//售信息表主键
                          
                        
                     }).ToList();
                var nn = SendMessage.Select().Where(p => p.ToUser.Replace(" ", "") == FromUserName).ToList();
                m = m.Where(p => p.MessageState.Replace(" ", "") == "已删除").ToList();
                var lis = (
                    from a in nn
                    join w in m
                    on a.MessageId equals w.MessageID
                    select new
                    {
                        zid = w.ReceiveID,
                        MessageDate = a.MessageDate,
                        MessageContent = a.MessageContent,
                        MessageTitle = a.MessageTitle,
                        SendState = "收信件状态：" + w.MessageState,
                        ToUser = a.ToUser,
                        FromUser = a.FromUser,
                        ReceiveID = w.ReceiveID,
                      
                    }).ToList();
                list.AddRange(lis);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            //单删
            if (Actioon == "del")
            {
                Guid id =Guid.Parse(context.Request["id"]);
                if (PoliceMessage.Select().Where(m => m.ReceiveID == id) != null)
                {
                    if (PoliceMessage.Delete(PoliceMessage.Select().Where(m => m.ReceiveID == id).FirstOrDefault()) > 0)
                    {
                        context.Response.Write(1);
                    }
                    else
                    {
                        context.Response.Write(0);
                    }
                }
                else
                {
                    if(SendMessage.Delete(SendMessage.Select().Where(m=>m.MessageId==id).FirstOrDefault())>0)
                    {
                        context.Response.Write(1);
                    }
                    else
                    {
                        context.Response.Write(0);
                    }
                    
                }
                
            }
            //批删
            if (Actioon == "dels")
            {
                string str = context.Request["zid"];
                str = str.Substring(0, str.Length - 1);
                string[] arr = str.Split(',');
                for (int i = 0; i <arr.Length; i++)
                {
                    if (PoliceMessage.Select().Where(m => m.ReceiveID == Guid.Parse(arr[i])) != null)
                    {
                        if (PoliceMessage.Delete(PoliceMessage.Select().Where(m => m.ReceiveID == Guid.Parse(arr[i])).FirstOrDefault()) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    else 
                    {
                        if (SendMessage.Delete(SendMessage.Select().Where(m => m.MessageId == Guid.Parse(arr[i])).FirstOrDefault()) > 0)
                        {
                           
                        }
                        else
                        {
                         
                        }

                    }
                   
                }
                context.Response.Write(1);


            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}